package com.groupbuying.provider.service.impl;

import com.groupbuying.provider.connector.FileFegin;
import com.groupbuying.provider.connector.SysconfigFeign;
import com.groupbuying.provider.constants.CommonConstants;
import com.groupbuying.provider.constants.StoreOpexRuleRelPreSearchConstants;
import com.groupbuying.provider.dao.StoreMainInfoDao;
import com.groupbuying.provider.domain.*;
import com.groupbuying.provider.dto.StorePhotoDTO;
import com.groupbuying.provider.dto.StoreShopTempDTO;
import com.groupbuying.provider.dto.convert.StoreShopDetailConvert;
import com.groupbuying.provider.service.*;
import com.groupbuying.provider.utils.apiresult.ApiResult;
import com.groupbuying.provider.utils.apiresult.AppApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wenguoqiang
 * @Email wguoqiango@163.com
 * @Date: 2020/1/7 0007 上午 11:25
 * @Des
 */
@Service
public class StoreMainInfoAppServiceImpl implements StoreMainInfoAppService {


    private static Logger log = LoggerFactory.getLogger(StoreMainInfoAppServiceImpl.class);

    @Autowired
    private StoreOpexRuleRelService storeOpexRuleRelService;
    @Autowired
    private StoreMainInfoDao storeMainInfoDao;
    @Autowired
    private StoreShopService storeShopService;
    @Autowired
    private FileFegin fileFegin;
    @Autowired
    private SysconfigFeign sysconfigFeign;
    @Autowired
    private StoreCheckoutCounterService storeCheckoutCounterService;

    @Override
    public ApiResult getShopDetail(Map<String, Object> params) {
        Object temp = params.get("storeUuid");
        String storeUuid = temp != null ? temp.toString() : null;
        if (StringUtils.isBlank(storeUuid)) {
            return AppApiResult.failure("params is null");
        }
        StoreMainInfoDO storeMainInfoDO = storeMainInfoDao.get(storeUuid);
        StoreShopDO storeShopDO = storeShopService.getByStoreUuid(storeUuid);

        if (storeShopDO == null) {
            AppApiResult.failure("门店不存在");
        }

        Map queryCounterParam = new HashMap();
        queryCounterParam.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        queryCounterParam.put("storeuuid", storeUuid);
        List<StoreCheckoutCounterDO> checkoutCounterDOList = storeCheckoutCounterService.list(queryCounterParam);
        String counterId = checkoutCounterDOList != null && checkoutCounterDOList.size() > 0 ? checkoutCounterDOList.get(0).getUuid(): null;

        StorePhotoDTO storePhotoDTO = new StorePhotoDTO();
        storePhotoDTO.setWithinPhoto(fileFegin.getDownloadUrlByFileGroupKey(storeShopDO.getWithinphoto()));
        storePhotoDTO.setDoorPhoto(fileFegin.getDownloadUrlByFileGroupKey(storeShopDO.getDoorphoto()));
        storePhotoDTO.setSlideshow(fileFegin.getDownloadUrlByFileGroupKey(storeShopDO.getSlideshow()));

        Map<String, Object> result = new HashMap<>();
        StoreShopTempDTO storeShopTempDTO = new StoreShopTempDTO();
        storeShopTempDTO.setStoreMainInfoDO(storeMainInfoDO);
        storeShopTempDTO.setStoreShopDO(storeShopDO);
        result.put("storePhotoDTO", storePhotoDTO);
        result.put("shopDetail", StoreShopDetailConvert.MAPPER.dTOToDTO(storeShopTempDTO));
        result.put("counterId",counterId);

        Map searchMap = new HashMap();
        searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_STOREID,storeUuid);
        searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_DELFLAG,CommonConstants.COMMON_FLAG_UNDEL);
        List <StoreOpexRuleRelDO> listOpex = storeOpexRuleRelService.list(searchMap);

        return AppApiResult.success(result);
    }
}
